﻿using MvvmCross.Core.ViewModels;
using Sidvall.IdentityManager.UI.Entities;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Sidvall.IdentityManager.UI.ViewModels
{
    public class InformationViewModel : Sidvall.IdentityManager.UI.ViewModels.Core.ViewModelBase
    {
        #region Public Members

        #region SubTitle

        public string SubTitle
        {
            get
            {
                return "About " + Sidvall.IdentityManager.Configuration.SettingsManager.ApplicationTitle;
            }
        }

        #endregion
        #region ServerContent

        public Sidvall.Security.ServerContent ServerContent
        {
            get
            {
                return Sidvall.IdentityManager.SystemContext.Current.ServerContent;
            }
        }

        #endregion
        #region ServerItem

        public ServerItem ServerItem
        {
            get
            {
                return Sidvall.IdentityManager.UI.SystemContext.Current.ActiveServerItem;
            }
        }

        #endregion
        #region Created

        private string _Created;
        public string Created
        {
            get
            {
                return _Created;
            }
            set
            {
                _Created = value;
                RaisePropertyChanged("Created");
            }
        }

        #endregion
        #region Version

        private string _Version;
        public string Version
        {
            get
            {
                return _Version;
            }
            set
            {
                _Version = value;
                RaisePropertyChanged("Version");
            }
        }

        #endregion
        #region FileVersion

        private string _FileVersion;
        public string FileVersion
        {
            get
            {
                return _FileVersion;
            }
            set
            {
                _FileVersion = value;
                RaisePropertyChanged("FileVersion");
            }
        }

        #endregion
        #region AboutText

        private string _AboutText;
        public string AboutText
        {
            get
            {
                return _AboutText;
            }
            set
            {
                _AboutText = value;
                RaisePropertyChanged("AboutText");
            }
        }

        #endregion

        // Commands
        #region ShowUserConfigurationCommand

        public ICommand ShowUserConfigurationCommand
        {
            get
            {
                return new MvxCommand(() => ShowUserConfiguration());
            }
        }

        #endregion
        #region ShowUserConfiguration

        public void ShowUserConfiguration()
        {
            ShowViewModel<UserConfigurationViewModel>((object)null, true, "CenterContent");
        }

        #endregion
        #region ShowNewsCommand

        public ICommand ShowNewsCommand
        {
            get
            {
                return new MvxCommand(ShowNews);
            }
        }

        #endregion
        #region ShowNews

        public void ShowNews()
        {
            ShowViewModel<NewsViewModel>((object)null, true, "CenterContent");
        }

        #endregion

        #region StartAsync

        protected override async Task StartAsync()
        {
            this.Version = Sidvall.IdentityManager.SystemContext.Current.Version;
            var tasks = GetLoadTasks();
            await System.Threading.Tasks.Task.WhenAll(tasks);
        }

        #endregion

        #endregion
        #region Private Members

        // Tasks
        #region GetLoadTasks

        private IEnumerable<Task> GetLoadTasks()
        {
            System.Collections.Generic.List<Task> tasks;

            tasks = new System.Collections.Generic.List<Task>();
            tasks.Add(LoadAssemblyTimestampAsync());
            tasks.Add(LoadAssemblyVersionAsync());
            tasks.Add(LoadAboutTextAsync());
            return tasks;
        }

        #endregion
        #region LoadAssemblyTimestampAsync

        private async Task LoadAssemblyTimestampAsync()
        {
            var time = await Sidvall.SystemContext.Current.PackageManager.GetInstalledDateAsync();
            this.Created = time.ToString();
        }

        #endregion
        #region LoadAssemblyVersionAsync

        private async Task LoadAssemblyVersionAsync()
        {
            var version = await Sidvall.SystemContext.Current.PackageManager.GetVersionAsync();
            this.FileVersion = version.ToString();
        }

        #endregion
        #region LoadAboutTextAsync

        private async Task LoadAboutTextAsync()
        {
            this.AboutText = await Sidvall.SystemContext.Current.SystemStorageManager.ReadAllTextAsync(@"Data\About.txt");
        }

        #endregion

        #endregion
    }
}
